package stark.app.function.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.stark.customview.turntable.Stk9CellTurnTableView;

import java.util.ArrayList;
import java.util.List;

import stark.app.R;
import stark.app.databinding.FragmentStk9TurnTableBinding;
import stark.common.basic.base.BaseNoModelFragment;

/**
 * Author：daizhongbin on 2023/9/8
 * Email：1027154338@qq.com
 * Desc:
 */
public class Stk9TurnTableFragment extends BaseNoModelFragment<FragmentStk9TurnTableBinding> {

    private boolean changeData = false;

    @Override
    protected int onCreate() {
        return R.layout.fragment_stk_9_turn_table;
    }

    @Override
    protected void initView() {
        Stk9CellAdapter adapter = new Stk9CellAdapter();
        adapter.setDataList(loadDatas(changeData));
        mDataBinding.turnTableView.setAdapter(adapter);
        mDataBinding.turnTableView.setListener(new Stk9CellTurnTableView.IListener() {
            @Override
            public void onRotateStart() {
                //开始转动，如需播放音效，可在这里开始播放音效
            }

            @Override
            public void onRotateEnd(int i) {
                //转动结束，可在这里停止播放音效
                mDataBinding.tvRet.setText("选中结果为：" + adapter.getItem(i));
            }
        });

        mDataBinding.btnChangeData.setOnClickListener(v -> {
            changeData = !changeData;
            adapter.setDataList(loadDatas(changeData));
        });
        mDataBinding.btnStarRotate.setOnClickListener(v -> {
            mDataBinding.turnTableView.start();
        });
    }

    @Override
    protected void initData() {
    }

    private List<String> loadDatas(boolean change) {
        List<String> datas = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            if (!change) {
                datas.add("数据" + i);
            } else {
                datas.add("测试" + i);
            }
        }
        return datas;
    }


    private static class Stk9CellAdapter extends Stk9CellTurnTableView.Adapter<String> {

        @Override
        protected View onCreateItemView(Context context, Stk9CellTurnTableView parent) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_stk_9_cell, null);
            return view;
        }

        @Override
        protected View onCreateCenterView(Context context, Stk9CellTurnTableView parent) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_stk_9_cell, null);
            view.setSelected(true);
            return view;
        }

        @Override
        protected void onBindItemView(View view, String text, boolean selected) {
            view.setSelected(selected);
            TextView tvName = view.findViewById(R.id.tvName);
            tvName.setText(text);
        }
    }

}
